package com.huluobo.higt4demo1911.ui.main;

import androidx.fragment.app.Fragment;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.huluobo.higt4demo1911.R;
import com.huluobo.higt4demo1911.base.StatusActivity;
import com.huluobo.higt4demo1911.core.mvp.IPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RxJava的关键条件
 * 观察者(Observer)和被观察者(Observable)
 * 通过订阅关系(subscribe)进行连接
 */

public class MainActivity extends StatusActivity<IPresenter> implements BottomBarLayout.OnItemSelectedListener {

    private BottomBarLayout bottomBarLayout;
    //构建对应页面Class
    private final List<Class<? extends Fragment>> fragmentList = new ArrayList<>();
    //页面缓存
    private final Map<Integer, Fragment> fragmentMap = new HashMap<>();

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        bottomBarLayout = findViewById(R.id.act_main_bottom_layout);
        bottomBarLayout.setOnItemSelectedListener(this);
    }

    @Override
    public void initData() {
        fragmentList.add(MainFragment.class);
        fragmentList.add(LiveFragment.class);
        fragmentList.add(DefFragment.class);
        fragmentList.add(DefFragment.class);
        fragmentList.add(DefFragment.class);

        bottomBarLayout.setCurrentItem(0);
    }

    //Fragment 上一次展示的下标
    private int myPreviousPosition = -1;

    @Override
    public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
        //展示页面 currentPosition
        //页面构建? fragmentMap中有没有当前的fragment
        if (!fragmentMap.containsKey(currentPosition)) {
            //FragmentMap中没有
            try {
                fragmentMap.put(currentPosition, fragmentList.get(currentPosition).newInstance());
            } catch (IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        }

        //页面构建完成 展示
        //页面之前展示过没有?
        if (fragmentMap.get(currentPosition).isAdded()) {
            //show
            if (myPreviousPosition > -1) {
                getSupportFragmentManager().beginTransaction()
                        .hide(fragmentMap.get(myPreviousPosition))
                        .show(fragmentMap.get(currentPosition)).commit();
            } else {
                getSupportFragmentManager().beginTransaction()
                        .show(fragmentMap.get(currentPosition)).commit();
            }
        } else {
            if (myPreviousPosition > -1) {
                //之前有展示过的页面
                getSupportFragmentManager().beginTransaction()
                        .hide(fragmentMap.get(myPreviousPosition))
                        .add(R.id.act_main_layout, fragmentMap.get(currentPosition))
                        .commit();
            } else {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.act_main_layout, fragmentMap.get(currentPosition)).commit();
            }
        }
        myPreviousPosition = currentPosition;
    }

    @Override
    protected void initBar() {
        statusTransBar();
//        statusBarColor(Color.BLUE);
//        fullScreen();
    }
}